package munon.manuapp;

import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.RelativeLayout;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Random;


public class ManuActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_manu);
    }

    public void playAlguien(View v) {
        MediaPlayer mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.alguien);
        mediaPlayer.start();
    }

    public void playDonde(View v) {
        MediaPlayer mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.dondeestais);
        mediaPlayer.start();
    }

    public void playAparcar(View v) {
        MediaPlayer mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.aparcar);
        mediaPlayer.start();
    }

    public void playVacaciones(View v) {
        MediaPlayer mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.vacaciones);
        mediaPlayer.start();
    }

    public void playNabo(View v) {
        MediaPlayer mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.nabo);
        mediaPlayer.start();
    }

    public void playMadrid(View v) {
        MediaPlayer mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.madrid);
        mediaPlayer.start();
    }

    public void shareVacaciones(View v) {
        InputStream in = getResources().openRawResource(R.raw.vacaciones);
        String filename = getResources().getResourceEntryName(R.raw.vacaciones);

        File f = new File(filename);
        File newSoundFile = new File(Environment.getExternalStorageDirectory() + "/copy.aac");

        if (!f.exists()) {
            try {
                OutputStream out = new FileOutputStream(newSoundFile);
                byte[] buffer = new byte[1024];
                int len;
                while ((len = in.read(buffer, 0, buffer.length)) != -1) {
                    out.write(buffer, 0, len);
                }
                in.close();
                out.close();
            } catch (FileNotFoundException e) {
                Log.i("Test", "Setup::copyResources - " + e.getMessage());
            } catch (IOException e) {
                Log.i("Test", "Setup::copyResources - " + e.getMessage());
            }
        }
        Intent shareIntent = new Intent();
        shareIntent.setAction(Intent.ACTION_SEND);
        shareIntent.setType("audio/*");
        shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(newSoundFile));
        startActivity(Intent.createChooser(shareIntent, "Compartir Manu Vacaciones"));
    }

    public void shareNabo(View v) {

        InputStream in = getResources().openRawResource(R.raw.nabo);
        String filename = getResources().getResourceEntryName(R.raw.nabo);

        File f = new File(filename);
        File newSoundFile = new File(Environment.getExternalStorageDirectory() + "/copy.aac");

        if (!f.exists()) {
            try {
                OutputStream out = new FileOutputStream(newSoundFile);
                byte[] buffer = new byte[1024];
                int len;
                while ((len = in.read(buffer, 0, buffer.length)) != -1) {
                    out.write(buffer, 0, len);
                }
                in.close();
                out.close();
            } catch (FileNotFoundException e) {
                Log.i("Test", "Setup::copyResources - " + e.getMessage());
            } catch (IOException e) {
                Log.i("Test", "Setup::copyResources - " + e.getMessage());
            }
        }
        Intent shareIntent = new Intent();
        shareIntent.setAction(Intent.ACTION_SEND);
        shareIntent.setType("audio/*");
        shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(newSoundFile));
//can share in any way - to other applications as well as bluetooth
        startActivity(Intent.createChooser(shareIntent, "Compartir Manu Nabo"));
//delete the copy
        //newSoundFile.delete();
    }

    public void shareMadrid(View v) {
        InputStream in = getResources().openRawResource(R.raw.madrid);
        String filename = getResources().getResourceEntryName(R.raw.madrid);

        File f = new File(filename);
        File newSoundFile = new File(Environment.getExternalStorageDirectory() + "/copy.aac");

        if (!f.exists()) {
            try {
                OutputStream out = new FileOutputStream(newSoundFile);
                byte[] buffer = new byte[1024];
                int len;
                while ((len = in.read(buffer, 0, buffer.length)) != -1) {
                    out.write(buffer, 0, len);
                }
                in.close();
                out.close();
            } catch (FileNotFoundException e) {
                Log.i("Test", "Setup::copyResources - " + e.getMessage());
            } catch (IOException e) {
                Log.i("Test", "Setup::copyResources - " + e.getMessage());
            }
        }
        Intent shareIntent = new Intent();
        shareIntent.setAction(Intent.ACTION_SEND);
        shareIntent.setType("audio/*");
        shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(newSoundFile));
        startActivity(Intent.createChooser(shareIntent, "Compartir Manu Madrid"));
    }

    public void shareAlguien(View v) {
        InputStream in = getResources().openRawResource(R.raw.alguien);
        String filename = getResources().getResourceEntryName(R.raw.alguien);

        File f = new File(filename);
        File newSoundFile = new File(Environment.getExternalStorageDirectory() + "/copy.aac");

        if (!f.exists()) {
            try {
                OutputStream out = new FileOutputStream(newSoundFile);
                byte[] buffer = new byte[1024];
                int len;
                while ((len = in.read(buffer, 0, buffer.length)) != -1) {
                    out.write(buffer, 0, len);
                }
                in.close();
                out.close();
            } catch (FileNotFoundException e) {
                Log.i("Test", "Setup::copyResources - " + e.getMessage());
            } catch (IOException e) {
                Log.i("Test", "Setup::copyResources - " + e.getMessage());
            }
        }
        Intent shareIntent = new Intent();
        shareIntent.setAction(Intent.ACTION_SEND);
        shareIntent.setType("audio/*");
        shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(newSoundFile));
        startActivity(Intent.createChooser(shareIntent, "Compartir Manu Alguien"));
    }

    public void shareAparcar(View v) {
        InputStream in = getResources().openRawResource(R.raw.aparcar);
        String filename = getResources().getResourceEntryName(R.raw.aparcar);

        File f = new File(filename);
        File newSoundFile = new File(Environment.getExternalStorageDirectory() + "/copy.aac");

        if (!f.exists()) {
            try {
                OutputStream out = new FileOutputStream(newSoundFile);
                byte[] buffer = new byte[1024];
                int len;
                while ((len = in.read(buffer, 0, buffer.length)) != -1) {
                    out.write(buffer, 0, len);
                }
                in.close();
                out.close();
            } catch (FileNotFoundException e) {
                Log.i("Test", "Setup::copyResources - " + e.getMessage());
            } catch (IOException e) {
                Log.i("Test", "Setup::copyResources - " + e.getMessage());
            }
        }
        Intent shareIntent = new Intent();
        shareIntent.setAction(Intent.ACTION_SEND);
        shareIntent.setType("audio/*");
        shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(newSoundFile));
        startActivity(Intent.createChooser(shareIntent, "Compartir Manu Aparcar"));
    }

    public void shareDonde(View v) {
        InputStream in = getResources().openRawResource(R.raw.dondeestais);
        String filename = getResources().getResourceEntryName(R.raw.dondeestais);

        File f = new File(filename);
        File newSoundFile = new File(Environment.getExternalStorageDirectory() + "/copy.aac");

        if (!f.exists()) {
            try {
                OutputStream out = new FileOutputStream(newSoundFile);
                byte[] buffer = new byte[1024];
                int len;
                while ((len = in.read(buffer, 0, buffer.length)) != -1) {
                    out.write(buffer, 0, len);
                }
                in.close();
                out.close();
            } catch (FileNotFoundException e) {
                Log.i("Test", "Setup::copyResources - " + e.getMessage());
            } catch (IOException e) {
                Log.i("Test", "Setup::copyResources - " + e.getMessage());
            }
        }
        Intent shareIntent = new Intent();
        shareIntent.setAction(Intent.ACTION_SEND);
        shareIntent.setType("audio/*");
        shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(newSoundFile));
        startActivity(Intent.createChooser(shareIntent, "Compartir Manu Donde estais?"));
    }


    public void changeWallpaper(View v) {

        Random rand = new Random();
        int randomNum = rand.nextInt((2 - 0) + 1) + 0;

        RelativeLayout mainRelativeLayout = (RelativeLayout) findViewById(R.id.mainLayout);

        switch (randomNum) {
            case 0:
                mainRelativeLayout.setBackgroundResource(R.drawable.wallpaper0);
                break;
            case 1:
                mainRelativeLayout.setBackgroundResource(R.drawable.wallpaper1);
                break;
            case 2:
                mainRelativeLayout.setBackgroundResource(R.drawable.wallpaper2);
                break;
        }

    }
}
